﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HotelManagement.BLO;

namespace HotelManagement.Services
{
    public partial class CustomerSelect : Form
    {

        private BindingSource _bindingSource;
        public CustomerSelect()
        {
            InitializeComponent();
            Init();
        }
        private void Init()
        {
            _bindingSource = new BindingSource();
            InitDataGridView();
        }

        private void InitDataGridView()
        {
            //Thêm cột chọn khách hàng
            DataGridViewButtonColumn select = new DataGridViewButtonColumn();
            select.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            select.Name = "SelectColumn";
            select.HeaderText = "Chọn";
            select.Text = "Select";
            select.Visible = true;
            select.UseColumnTextForButtonValue = true;
            select.FlatStyle = FlatStyle.Popup;
            select.CellTemplate.Style.BackColor = Color.CadetBlue;
            this.dataGridViewCustomerSelect.Columns.Insert(0, select);

            //dataGridViewCustomerSelect.Columns.Add("Name", "Họ tên");
            //dataGridViewCustomerSelect.Columns.Add("Gender", "Giới tính");
            //dataGridViewCustomerSelect.Columns.Add("IDNo", "CMND");

            //Đọc dữ liệu khách đan ở vào datagridview
            RentDetailBLO bloRD = new RentDetailBLO();
            _bindingSource.DataSource = bloRD.GetAllCustomerOfTheHotel();
            if (_bindingSource.DataSource != null)
            {
                _bindingSource.Filter = "";
                dataGridViewCustomerSelect.DataSource = _bindingSource;
            }
            foreach (DataGridViewColumn col in dataGridViewCustomerSelect.Columns)
            {
                if (col.Index>4)
                {
                    col.Visible = false;
                }
            }
        }
        

        #region "Tìm kiếm khách hàng"
        private string GetFilterExpression()
        {
            if (txtNameIDNo.Text=="")
            {
                return "";
            }
            string filter = "";

            filter += "Name like '%" + txtNameIDNo.Text + "%' OR IDNo like '%" + txtNameIDNo.Text + "%'";
            return filter;
        }
        #endregion

        private void txtNameIDNo_TextChanged(object sender, EventArgs e)
        {
            _bindingSource.Filter = GetFilterExpression();
            dataGridViewCustomerSelect.DataSource = _bindingSource;
        }

        private void dataGridViewCustomerSelect_CellClick(object sender, DataGridViewCellEventArgs e)
        {

            DataGridViewRow row = new DataGridViewRow();
            try
            {
                row = dataGridViewCustomerSelect.Rows[e.RowIndex];
                string colN = dataGridViewCustomerSelect.Columns[e.ColumnIndex].Name;
                if (colN == "SelectColumn")
                {
                    ServiceManagement._customer.CustomerID = int.Parse(row.Cells["CustomerID"].Value.ToString());
                    ServiceManagement._customer.Name = row.Cells["Name"].Value.ToString();
                    ServiceManagement._customer.IDNo_Passport = row.Cells["IDNo"].Value.ToString();
                    ServiceManagement._customer.CusTypeID = int.Parse(row.Cells["CusTypeID"].Value.ToString());
                    ServiceManagement._customer.BirthYear = DateTime.Parse(row.Cells["Birth"].Value.ToString());
                    ServiceManagement._customer.Gender = row.Cells["Gender"].Value.ToString();
                    ServiceManagement._customer.Nationality = row.Cells["Nationality"].Value.ToString();
                    ServiceManagement._customer.Occupation = row.Cells["Occupation"].Value.ToString();
                    ServiceManagement._customer.PhoneNumber = row.Cells["PhoneNumber"].Value.ToString();
                    ServiceManagement._customer.Address = row.Cells["Address"].Value.ToString();
                    ServiceManagement._rentID = int.Parse(row.Cells["rentid"].Value.ToString());
                    this.DialogResult = DialogResult.OK;
                }
            }
            catch (System.Exception el)
            {
                this.DialogResult = DialogResult.Cancel;
                return;
            }
        }
    }
}
